<?php
/**
 * [YIZHIKEJI System] Copyright (c) 2014 yz929.cn
 * YIZHIKEJI is NOT a free software, it under the license terms, visited http://www.yz929.cn/ for more details.
 */
defined('IN_IA') or exit('Access Denied');
$_W['page']['title'] = '站点管理 - 微站功能';
uni_user_permission_check('site_multi_display');
load()->model('site');
$dos = array('display', 'post', 'del', 'default', 'copy');
$do = in_array($do, $dos) ? $do : 'display';
$setting = uni_setting($_W['uniacid'], 'default_site');
$default_site = intval($setting['default_site']);
uni_user_permission_check('site_multi_post');
$id = intval($_GPC['multiid']);
if (!empty($id)) {
    $multi = pdo_fetch('SELECT * FROM ' . tablename('site_multi') . ' WHERE uid = :uid AND id = :id', array(':uid' => $_W['uid'], ':id' => $id));
    if (empty($multi)) {
        message('微站不存在或已删除', referer(), 'error');
    }
    $multi['site_info'] = iunserializer($multi['site_info']) ? iunserializer($multi['site_info']) : array();
}
$sql = 'SELECT `s`.*, `t`.`name` AS `tname`, `t`.`title` FROM ' . tablename('site_styles') . ' AS `s` LEFT JOIN ' .
    tablename('site_templates') . ' AS `t` ON `s`.`templateid` = `t`.`id` WHERE `s`.`uid` = :uid';
$styles = pdo_fetchall($sql, array(':uid' => $_W['uid']), 'id');
$multi['style'] = $styles[$multi['styleid']];

if (checksubmit('submit')) {
    if (checksubmit('submit')) {
        $data = array(
            'uid' => $_W['uid'],
            'title' => trim($_GPC['title']),
            'styleid' => intval($_GPC['styleid']),
            'status' => intval($_GPC['status']),
            'site_info' => iserializer(array(
                'thumb' => $_GPC['thumb'],
                'keyword' => $_GPC['keyword'],
                'description' => $_GPC['description'],
                'footer' => htmlspecialchars_decode($_GPC['footer'])
            )),
            'bindhost' => $_GPC['bindhost'],
        );
        if (empty($data['title'])) {
            message('请填写站点名称', referer(), 'error');
        }
//        if (!strcasecmp($_GPC['bindhost'], $_W['page']['copyright']['sitehost'])) {
//            message('请填写其他域名，不要填本平台域名（或者后台-站点设置域名项没填）！', referer(), 'error');
//        }
        if (!empty($id)) {
            pdo_update('site_multi', $data, array('id' => $id));
        } else {
            pdo_insert('site_multi', $data);
            $id = pdo_insertid();
        }
        if (!empty($_GPC['keyword'])) {
            $cover = array(
                'uid' => $_W['uid'],
                'title' => $data['title'],
                'keyword' => $_GPC['keyword'],
                'url' => url('home', array('i' => $_W['uid'], 't' => $id)),
                'description' => $_GPC['description'],
                'thumb' => $_GPC['thumb'],
                'module' => 'site',
                'multiid' => $id,
            );
            site_cover($cover);
        }
        message('更新站点信息成功！', url('account/weizhan'), 'success');
    }
}
template('weizhan/edit');